package com.softeng.network.chess;


import javax.swing.JFrame;


public class Rook extends ChessPiece{
	
	public boolean firstMove; 
	
	//-----------------------------------------------------------------------------------------------
	
	Rook(String color){
		
		idString = "rook";
		this.color = color;
		this.firstMove = true;
		
		if (color == "white"){
			this.pieceString = "resources/assets/rook_white.gif";
		}else{
			this.pieceString = "resources/assets/rook_black.gif";
		}

	}
	
	//-----------------------------------------------------------------------------------------------
	
	Rook(String color, int position, JFrame window){

		this(color);
		
		addPieceImage(position, window, this.pieceString);
		
	}
	
	//-----------------------------------------------------------------------------------------------
	
	// Sees if a particular move is a move that a Rook is allowed to make
	public boolean validMove(int currentPosition, int position, ChessPiece[] boardArray){
		
		boolean validMove = false;
		int currentRow = getRowFromPos(currentPosition);
		int currentCol = getColFromPos(currentPosition);
		int nextRow = getRowFromPos(position);
		int nextCol = getColFromPos(position); 
		
		if((currentRow == nextRow || currentCol == nextCol) && checkPiecesBetween(currentPosition,position,boardArray) == false){
			validMove = true; 
			System.out.println("Rook moves, valid move = true");
			this.firstMove = false;
		}
		
		return validMove; 
		
	}
	
	//-----------------------------------------------------------------------------------------------

	//checks if there are pieces between the original and destination squares
	public boolean checkPiecesBetween(int currentPosition, int position, ChessPiece[] boardArray){
		
		boolean checkPiecesBetween = false;
		
		int currentRow = getRowFromPos(currentPosition);
		int currentCol = getColFromPos(currentPosition);
		int nextRow = getRowFromPos(position);
		int nextCol = getColFromPos(position); 
		
		int n; 
		
		//if moving horizontally
		if(currentRow == nextRow){ 
			//if moving to the right
			if(currentCol < nextCol){
				for (n = currentCol+1; n < nextCol; n++){
					if(boardArray[rowCol2Position(currentRow, n)]!=null){
						checkPiecesBetween = true; 
						System.out.println("Attempted to move right but found a piece in the way");
					}
				}
			}
			//if moving to the left
			if(currentCol > nextCol){
				for (n = currentCol-1; n > nextCol; n--){
					if(boardArray[rowCol2Position(currentRow, n)]!=null){
						checkPiecesBetween = true; 
						System.out.println("Attempted to move left but found a piece in the way");
					}
				}
			}
			
		}
		//if moving vertically
		else if(currentCol == nextCol){
			//if moving up the board
			if(currentRow < nextRow){
				for (n = currentRow+1; n < nextRow; n++){
					if(boardArray[rowCol2Position(n, currentCol)]!=null){
						checkPiecesBetween = true;
						System.out.println("Attempted to move forward but found a piece in the way");
					}
				}
			}
			//if moving down the board
			if(currentRow > nextRow){
				for (n = currentRow-1; n > nextRow; n--){
					if(boardArray[rowCol2Position(n, currentCol)]!=null){
						checkPiecesBetween = true;
						System.out.println("Attempted to move backward but found a piece in the way");
					}
				}
			}
		}
		
		return checkPiecesBetween; 
		
	}
		
}
